<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/19  14:05
// +----------------------------------------------------------------------
namespace Wechat\Controller;
//评论
class CommentController extends BaseController {
    //点评记录-全部
    function all() {
        $apprise = M("order_book_apprise");
        $user_id = session("user_id");
        $d = $apprise->alias("a")->where("user_id=$user_id")->join("book b on a.book_id=b.book_id", "LEFT")->field("a.*,b.b_logo,b.sm")->select();
        $this->assign("d", $d);
        $this->display();
    }

    function pic() {
        $this->display();
    }

    //填写评论页面
    function write() {
        $order_id = I("get.id");
        //获取当前order 的图书中未评价的图书
        $order_has_book = M("Order_has_book");
        $d = $order_has_book->alias("a")->where("order_id=$order_id")->join("book b on a.book_id=b.book_id", "LEFT")->field("a.*,b.b_logo")->order("is_apprised")->select();
        $this->assign("d", $d);
        $this->display();
    }
    //评论提交
    //参数：
    //      内容-comcontent=asd
    //      评分-内容相符-describe=2
    //      评分-发货速度-speed_trans=3
    //      评分-服务态度-attitude_service=3
    //      order_has_book的id-order_has_book_id=20
    //      评论的书本-book_id=8
    //      订单id-order_id
    function post() {
        $apprise = M("Order_book_apprise");
        $order_id = I("get.order_id");
        $data['order_has_book_id'] = I("get.order_has_book_id");
        $data['book_id'] = I("get.book_id");
        $count = $apprise->where($data)->count();
        if ($count) {
            echo 0;
            return;
        }
        $data['content'] = sensitivecheck(I("get.comcontent")); //敏感词汇检测
        $data['score_describe'] = I("get.describe");
        $data['score_transpeed'] = I("get.speed_trans");
        $data['score_server'] = I("get.attitude_service");
        $data['user_id'] = session("user_id");
        $apprise->startTrans();
        $appid = $apprise->data($data)->add();
        if ($appid) {
            $apprise->commit();
            $mapd['order_has_book_id'] = $data['order_has_book_id'];
            $mapd['book_id'] = $data['book_id'];
            $orderbook = M("Order_has_book");
            $datad['is_apprised'] = 1;
            $orderbook->where($mapd)->save($datad);
            $do = $orderbook->where($mapd)->field("type")->find();
            switch ($do["type"]) {
                case 0:
                    $type = 2;
                    break;//购书
                case 1:
                    $type = 1;
                    break;//借阅
            }
            $Score = D("Score");
            $number = M('order_has_book')->where(['order_has_book_id' => I("get.order_has_book_id")])->getField('quantity');
            $r=$Score->addscore($data['user_id'], $type, $number);
            echo $r;    //输出积分数
        }else{
            $apprise->rollback();
            echo 0;
        }
    }

    //视频评论提交
    function post_video() {
        $video_id = I("post.id");
        $content = sensitivecheck(I("post.content"));
        if ($video_id == "") {
            echo 0;
            return;
        }
        $Apr = M("Video_apprise");
        $data['video_id'] = $video_id;
        $data['user_id'] = session("user_id");
        $c=$Apr->where($data)->count();
        $data['content'] = $content;
        $Apr->startTrans();
        $com_id = $Apr->data($data)->add();
        if ($com_id) {
            if($c>0){
                $j=[
                    'status'=>'success',
                    'data'=>'评论成功,获得0积分'
                ];
                $this->ajaxReturn($j);
                return;
            }
            $Score = D("Score");
            $r=$Score->addscore($data['user_id'], "3");
            if($r){
                $Apr->commit();
                $j=[
                    'status'=>'success',
                    'data'=>"评论成功,获得{$r}积分"
                ];
            }else{
                $Apr->rollback();
                $j=[
                    'status'=>'error',
                    'data'=>'评论失败'
                ];
            }
        } else {
            $Apr->rollback();
            $j=[
                'status'=>'error',
                'data'=>'评论失败'
            ];
        }
        $this->ajaxReturn($j);
    }
}